package com.lifeservice.console.service.impl;

import com.lifeservice.common.core.redis.RedisCache;
import com.lifeservice.common.exception.ServiceException;
import com.lifeservice.common.utils.SecurityUtils;
import com.lifeservice.common.utils.SnowFlakeUtil;
import com.lifeservice.common.utils.StringUtils;
import com.lifeservice.console.domain.LifeSupplierDraft;
import com.lifeservice.console.domain.LifeSupplierInfo;
import com.lifeservice.console.domain.req.LifeSupplierDraftReq;
import com.lifeservice.console.mapper.LifeSupplierInfoMapper;
import com.lifeservice.console.service.ILifeSupplierInfoService;
import com.lifeservice.util.LifeServiceConstants;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 商户信息Service业务层处理
 *
 * @author lifeservice
 * @date 2023-02-25
 */
@Service
public class LifeSupplierInfoServiceImpl implements ILifeSupplierInfoService {
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private LifeSupplierInfoMapper lifeSupplierInfoMapper;

    /**
     * 查询商户信息
     *
     * @param id 商户信息主键
     * @return 商户信息
     */
    @Override
    public LifeSupplierInfo selectLifeSupplierInfoById(String id) {
        LifeSupplierInfo cacheInfo = redisCache.getCacheObject(LifeServiceConstants.SUPPLIER_INFO_CACHE_PREFIX + id);
        if (null != cacheInfo) {
            return cacheInfo;
        }
        cacheInfo = lifeSupplierInfoMapper.selectLifeSupplierInfoById(id);
        redisCache.setCacheObject(LifeServiceConstants.SUPPLIER_INFO_CACHE_PREFIX + id, cacheInfo, 1, TimeUnit.DAYS);
        return cacheInfo;
    }

    @Override
    public LifeSupplierDraft selectLifeSupplierDraftById(String id) {
        LifeSupplierDraft lifeSupplierDraft = redisCache.getCacheObject(LifeServiceConstants.SUPPLIER_DRAFT_CACHE_PREFIX + id);
        if (null != lifeSupplierDraft) {
            return lifeSupplierDraft;
        }
        lifeSupplierDraft = lifeSupplierInfoMapper.selectLifeSupplierDraftById(id);
        redisCache.setCacheObject(LifeServiceConstants.SUPPLIER_DRAFT_CACHE_PREFIX + id, lifeSupplierDraft, 1, TimeUnit.DAYS);
        return lifeSupplierDraft;
    }

    /**
     * 查询商户信息列表
     *
     * @param lifeSupplierInfo 商户信息
     * @return 商户信息
     */
    @Override
    public List<LifeSupplierInfo> selectLifeSupplierInfoList(LifeSupplierInfo lifeSupplierInfo) {
        return lifeSupplierInfoMapper.selectLifeSupplierInfoList(lifeSupplierInfo);
    }

    /**
     * 新增商户信息
     *
     * @param lifeSupplierInfo 商户信息
     * @return 结果
     */
    @Override
    public int insertLifeSupplierInfo(LifeSupplierInfo lifeSupplierInfo) {
        if (StringUtils.isNotEmpty(lifeSupplierInfoMapper.selectLifeSupplierInfoBySupplierName(lifeSupplierInfo.getSupplierName()))) {
            throw new ServiceException("供应商名称重复");
        }
        lifeSupplierInfo.setId(SnowFlakeUtil.getInstance().nextId());
        lifeSupplierInfo.setServiceId(lifeSupplierInfo.getServiceId());
        lifeSupplierInfo.setCreateTime(new Date());
        lifeSupplierInfo.setUpdateTime(new Date());
        lifeSupplierInfo.setScore(new BigDecimal(0));
        String createBy = StringUtils.isEmpty(lifeSupplierInfo.getCreateBy()) ? SecurityUtils.getLoginUser().getUsername() : lifeSupplierInfo.getCreateBy();
        lifeSupplierInfo.setCreateBy(createBy);
        redisCache.setCacheObject(LifeServiceConstants.SUPPLIER_INFO_CACHE_PREFIX + lifeSupplierInfo.getId(), lifeSupplierInfo);
        return lifeSupplierInfoMapper.insertLifeSupplierInfo(lifeSupplierInfo);
    }

    @Override
    public int addLifeSupplierDraft(LifeSupplierDraft lifeSupplierDraft) {
        if (StringUtils.isNotEmpty(lifeSupplierInfoMapper.selectLifeSupplierDraftInfoBySupplierName(lifeSupplierDraft.getSupplierName()))) {
            throw new ServiceException("供应商名称重复");
        }
        lifeSupplierDraft.setId(SnowFlakeUtil.getInstance().nextId());
        lifeSupplierDraft.setCreateTime(new Date());
        lifeSupplierDraft.setUpdateTime(new Date());
        lifeSupplierDraft.setScore(new BigDecimal(0));
        lifeSupplierDraft.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        lifeSupplierDraft.setStatus(0);
        redisCache.setCacheObject(LifeServiceConstants.SUPPLIER_DRAFT_CACHE_PREFIX + lifeSupplierDraft.getId(), lifeSupplierDraft);
        return lifeSupplierInfoMapper.addLifeSupplierDraft(lifeSupplierDraft);
    }

    @Override
    public void reviewMerchant(LifeSupplierDraftReq lifeSupplierDraftReq) {
        if (StringUtils.isEmpty(lifeSupplierDraftReq.getId())) {
            return;
        }
        String id = lifeSupplierDraftReq.getId();
        LifeSupplierDraft lifeSupplierDraft = lifeSupplierInfoMapper.selectLifeSupplierDraftById(id);
        //查看状态是否修改
        if (lifeSupplierDraft.getStatus().equals(lifeSupplierDraftReq.getStatus())) {
            return;
        }
        //审核通过
        if (lifeSupplierDraftReq.getStatus() == 1) {
            LifeSupplierInfo lifeSupplierInfo = new LifeSupplierInfo();
            BeanUtils.copyProperties(lifeSupplierDraft, lifeSupplierInfo);
            // 判断是否已存在，如果存在则更新
            LifeSupplierInfo isExit = lifeSupplierInfoMapper.selectLifeSupplierInfoById(lifeSupplierInfo.getId());
            if (null == isExit) {
                lifeSupplierInfoMapper.insertLifeSupplierInfo(lifeSupplierInfo);
            } else {
                lifeSupplierInfoMapper.updateLifeSupplierInfo(lifeSupplierInfo);
            }
        }

        //修改商户的状态
        lifeSupplierInfoMapper.updateStatus(lifeSupplierDraftReq);

    }


    /**
     * 修改商户信息
     *
     * @param lifeSupplierInfo 商户信息
     * @return 结果
     */
    @Override
    public int updateLifeSupplierInfo(LifeSupplierInfo lifeSupplierInfo) {
        lifeSupplierInfo.setUpdateTime(new Date());
        lifeSupplierInfo.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        if (StringUtils.isEmpty(lifeSupplierInfo.getSupplierDesc())) {
            lifeSupplierInfo.setSupplierDesc(null);
        }
        if (StringUtils.isEmpty(lifeSupplierInfo.getSupplierDetailAddress())) {
            lifeSupplierInfo.setSupplierDetailAddress(null);
        }
        if (StringUtils.isEmpty(lifeSupplierInfo.getSupplierServiceDetailText())) {
            lifeSupplierInfo.setSupplierServiceDetailText(null);
        }
        if (StringUtils.isEmpty(lifeSupplierInfo.getSupplierServiceDetailIcon())) {
            lifeSupplierInfo.setSupplierServiceDetailIcon(null);
        }
        redisCache.deleteObject(LifeServiceConstants.SUPPLIER_INFO_CACHE_PREFIX + lifeSupplierInfo.getId());
        redisCache.setCacheObject(LifeServiceConstants.SUPPLIER_INFO_CACHE_PREFIX + lifeSupplierInfo.getId(), lifeSupplierInfo);
        return lifeSupplierInfoMapper.updateLifeSupplierInfo(lifeSupplierInfo);
    }

    @Override
    public int updateLifeSupplierDraft(LifeSupplierDraft lifeSupplierDraft) {
        lifeSupplierDraft.setUpdateTime(new Date());
        lifeSupplierDraft.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        if (StringUtils.isEmpty(lifeSupplierDraft.getSupplierDesc())) {
            lifeSupplierDraft.setSupplierDesc(null);
        }
        if (StringUtils.isEmpty(lifeSupplierDraft.getSupplierDetailAddress())) {
            lifeSupplierDraft.setSupplierDetailAddress(null);
        }
        if (StringUtils.isEmpty(lifeSupplierDraft.getSupplierServiceDetailText())) {
            lifeSupplierDraft.setSupplierServiceDetailText(null);
        }
        if (StringUtils.isEmpty(lifeSupplierDraft.getSupplierServiceDetailIcon())) {
            lifeSupplierDraft.setSupplierServiceDetailIcon(null);
        }
        redisCache.deleteObject(LifeServiceConstants.SUPPLIER_DRAFT_CACHE_PREFIX + lifeSupplierDraft.getId());
        redisCache.setCacheObject(LifeServiceConstants.SUPPLIER_DRAFT_CACHE_PREFIX + lifeSupplierDraft.getId(), lifeSupplierDraft);
        lifeSupplierDraft.setStatus(0);
        return lifeSupplierInfoMapper.updateLifeSupplierDraft(lifeSupplierDraft);
    }

    @Override
    public List<LifeSupplierInfo> selectLifeSupplierInfoByName(String username) {
        if (StringUtils.isEmpty(username)) {
            return null;
        }
        return lifeSupplierInfoMapper.selectLifeSupplierInfoByName(username);
    }

    /**
     * 批量删除商户信息
     *
     * @param ids 需要删除的商户信息主键
     * @return 结果
     */
    @Override
    public int deleteLifeSupplierInfoByIds(String[] ids) {
        for (String id : ids) {
            redisCache.deleteObject(LifeServiceConstants.SUPPLIER_INFO_CACHE_PREFIX + id);
        }
        return lifeSupplierInfoMapper.deleteLifeSupplierInfoByIds(ids);
    }

    @Override
    public int deleteLifeSupplierDraftByIds(String[] ids) {
        for (String id : ids) {
            redisCache.deleteObject(LifeServiceConstants.SUPPLIER_DRAFT_CACHE_PREFIX + id);
        }
        return lifeSupplierInfoMapper.deleteLifeSupplierDraftByIds(ids);
    }

    /**
     * 删除商户信息信息
     *
     * @param id 商户信息主键
     * @return 结果
     */
    @Override
    public int deleteLifeSupplierInfoById(String id) {
        redisCache.deleteObject(LifeServiceConstants.SUPPLIER_INFO_CACHE_PREFIX + id);
        return lifeSupplierInfoMapper.deleteLifeSupplierInfoById(id);
    }

    @Override
    public List<LifeSupplierDraft> lifeServiceDraft(LifeSupplierDraftReq lifeSupplierDraftReq) {
        String username = SecurityUtils.getLoginUser().getUsername();
        Long userId = SecurityUtils.getLoginUser().getUserId();
        List<LifeSupplierDraft> lifeSupplierDrafts = lifeSupplierInfoMapper.lifeServiceDraft(lifeSupplierDraftReq, username, userId);
        return lifeSupplierDrafts;
    }


}
